package com.bite.book.service;

import com.bite.book.enums.BookStatusEnum;
import com.bite.book.mapper.BookMapper;
import com.bite.book.model.BookInfo;
import com.bite.book.model.PageRequest;
import com.bite.book.model.PageResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class BookService {
    @Autowired
    private BookMapper bookMapper;

    public Integer insertBook(BookInfo bookInfo) {
        return bookMapper.insertBook(bookInfo);
    }

    public PageResponse<BookInfo> getListByPage(PageRequest pageRequest) {
        //1. 总记录数
        Integer count = bookMapper.count();
        //2. 当前页的记录
        List<BookInfo> bookInfos = bookMapper.queryBookByPage(pageRequest);
        for (BookInfo bookInfo: bookInfos){
            bookInfo.setStatusCN(BookStatusEnum.getStatusByCode(bookInfo.getStatus()).getDesc());
        }
        return new PageResponse<BookInfo>(count, bookInfos, pageRequest);

    }

    public BookInfo queryBookById(Integer bookId) {
        return bookMapper.queryBookById(bookId);
    }

    public Integer updateBook(BookInfo bookInfo) {
        return bookMapper.updateBook(bookInfo);
    }

    public void batchDelete(List<Integer> ids) {
        bookMapper.batchDelete(ids);
    }
}
